package client.gui;

import interfacep.TeacherReqeust;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.*;

import messages.MessageAllExams;
import messages.MessageNewTeacherRequests;
import messages.MessageRequestDecision;

import sharedspace.Teacher;
import sharedspace.Test;
import sharedspace.User;

import client.gui.GUI_AbstractMainMenu.MainMenuType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
/**
 * This class is a panel that responsible to show all the manager options
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Manager extends GUI_AbstractMainMenu{
	private JLabel lblRequests = null;	
	private JButton buViewQuestions = null;
	private JButton buViewExams = null;
	private JButton buViewGrades = null;
	private JButton buStatisticalReports = null;
	private JButton buAcceptButton = null;
	private JButton buRejectButton = null;
	private JButton buRefresh = null;
	private JList liTeacherRequests = null;
	private DefaultListModel requestList = null;
	private ArrayList<TeacherReqeust> trList = null;

	public GUI_Manager_ViewQuestions exs = null;	
	public GUI_Manager_ViewExams exss = null;	
	public Client_GUI ms=null;
	
	public GUI_Manager(Client_GUI ms) {
		super(MainMenuType.MANAGER,ms);
		this.ms=ms;
		initiateManager();
		greetMsg.setText("Welcome "+ms.lg.getCurrentUsername()+"!");
		mainMenu.setText("Manager");
		

	}
	/**
	 * Initiate the panel contents 
	 */
	private void initiateManager()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbuViewQuestions(), null);
		
		background.add(getbuViewQuestions(),null);
		background.add(getbuViewExams(),null);
		background.add(getbuViewGrades(),null);
		background.add(getbuStatisticalReports(),null);
		background.add(getliTeacherRequests(),null);
		background.add(liTeacherRequests);
		background.add(lblRequests);
		background.add(buAcceptButton);
		background.add(buRejectButton);
		background.add(buRefresh);
	}
	
	private JButton getbuViewQuestions()
	{
		if (buViewQuestions == null)
		{
			//set graphics
			buViewQuestions = new JButton();
			buViewQuestions.setFont(new Font("Arial", Font.PLAIN, 16));
			buViewQuestions.setBounds(new Rectangle(53, 210, 146, 34));
			buViewQuestions.setText("View Questions");
			
			//button "buViewQuestions" pressed
			this.buViewQuestions.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ms.mvq = new GUI_Manager_ViewQuestions(ms); 
						ms.setContentPane(ms.mvq);
						ms.mvq.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.mngr);
							}});
									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
						}
				}
			}
			);
			
		}
		
		return buViewQuestions;
	}
	
	private JButton getbuViewExams()
	{
		if (buViewExams == null)
		{
			//set graphics
			buViewExams = new JButton();
			buViewExams.setFont(new Font("Arial", Font.PLAIN, 16));
			buViewExams.setBounds(new Rectangle(228, 210, 146, 34));
			buViewExams.setText("View Exams");
			//button "buViewExams" pressed
			this.buViewExams.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ms.mve = new GUI_Manager_ViewExams(ms); 
						ms.setContentPane(ms.mve);
						ms.mve.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.mngr);
							}});
									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
						}
				}
			}
			);
			
		}
		
		return buViewExams;
	}
	

	
	private JButton getbuViewGrades()
	{
		if (buViewGrades == null)
		{
			//set graphics
			buViewGrades = new JButton();
			buViewGrades.setFont(new Font("Arial", Font.PLAIN, 16));
			buViewGrades.setBounds(new Rectangle(406, 210, 146, 34));
			buViewGrades.setText("View Grades");
			//button "buViewGrades" pressed
			this.buViewGrades.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ms.mvg = new GUI_Manager_ViewGrades(ms); 
						ms.setContentPane(ms.mvg);
						ms.mvg.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.mngr);
							}});
									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
						}
				}
			}
			);
			
		}
		
		return buViewGrades;
	}
	
	private JButton getbuStatisticalReports()
	{
		if (buStatisticalReports == null)
		{
			//set graphics
			buStatisticalReports = new JButton();
			buStatisticalReports.setFont(new Font("Arial", Font.PLAIN, 16));
			buStatisticalReports.setBounds(new Rectangle(582, 210, 208, 34));
			buStatisticalReports.setText("Statistical Reports");
			this.buStatisticalReports.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ms.mst = new GUI_Manager_Statistics(ms); 
						ms.setContentPane(ms.mst);
						ms.mst.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.mngr);
							}});
									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
						}
				}
			}
			);
			
		}
		
		return buStatisticalReports;
	}
	
	private JList getliTeacherRequests()
	{
		int i;
		if (liTeacherRequests == null)
		{
			MessageNewTeacherRequests msgNTR=new MessageNewTeacherRequests();
			try {
				ms.client.sendToServer(msgNTR);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			trList = (ArrayList<TeacherReqeust>)ms.client.GetMessage();
			requestList = new DefaultListModel();
			for (i=0; i<trList.size(); i++)
			{
				requestList.addElement("Exam ID:  " + trList.get(i).getExecuteTestID() + "   Extra Minutes: " + trList.get(i).getExtraTime()  +  "   Reason: " + trList.get(i).getTeacherRequestChange());
			}
			liTeacherRequests = new JList(requestList);
			liTeacherRequests.setVisibleRowCount(5);
			liTeacherRequests.setBounds(53, 321, 675, 155);
			buAcceptButton = new JButton("Accept");
			buAcceptButton.setBounds(53, 487, 89, 23);
			buAcceptButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int i = liTeacherRequests.getSelectedIndex();
					MessageRequestDecision msgUTR=new MessageRequestDecision(trList.get(liTeacherRequests.getSelectedIndex()).getExecuteTestID(),1,trList.get(liTeacherRequests.getSelectedIndex()).getExtraTime());
					try {
						ms.client.sendToServer(msgUTR);
					} catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					requestList.remove(liTeacherRequests.getSelectedIndex());
				}
			}
			);			
			buRejectButton = new JButton("Reject");
			buRejectButton.setBounds(152, 487, 89, 23);
			buRejectButton.addActionListener(new java.awt.event.ActionListener() {
				int i;
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					MessageRequestDecision msgUTR=new MessageRequestDecision(trList.get(liTeacherRequests.getSelectedIndex()).getExecuteTestID(),0,trList.get(liTeacherRequests.getSelectedIndex()).getExtraTime());
					try {
						ms.client.sendToServer(msgUTR);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					requestList.remove(liTeacherRequests.getSelectedIndex());
				}
			}
			);	
			lblRequests = new JLabel("Requests:");
			lblRequests.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 24));
			lblRequests.setForeground(Color.BLUE);
			lblRequests.setBounds(53, 287, 237, 34);
			
			buRefresh = new JButton("Refresh");
			buRefresh.setBounds(639, 487, 89, 23);
			buRefresh.addActionListener(new java.awt.event.ActionListener() {
				int i;
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					requestList.clear();
					MessageNewTeacherRequests msgNTR=new MessageNewTeacherRequests();
					try {
						ms.client.sendToServer(msgNTR);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					trList = (ArrayList<TeacherReqeust>)ms.client.GetMessage();
					for (i=0; i<trList.size(); i++)
					{
						requestList.addElement("Exam ID:  " + trList.get(i).getExecuteTestID() + "   Extra Minutes: " + trList.get(i).getExtraTime()  +  "   Reason: " + trList.get(i).getTeacherRequestChange());
					}
					//liTeacherRequests = new JList(requestList);
				}
			}
			);
	}
		return liTeacherRequests;
	}
}
